function ab(grid) {
  let m = grid.length, n = grid[0].length
  let arr1 = []
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (grid[i][j] == 1) {
        let ab = []
        dfs(i, j, ab, null)
        arr1.push(ab)
      }
    }
  }

  function dfs(i, j, arr, sign) {
    if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] === 0) return
    grid[i][j] = 0
    if (sign) {
      arr.push(sign)
    }

    dfs(i, j + 1, arr, 1)
    dfs(i, j - 1, arr, 2)
    dfs(i + 1, j, arr, 3)
    dfs(i - 1, j, arr, 4)
    arr.push(-sign)
  }
  let arr2 = arr1.map(item => item.join(''))
  let res = [...new Set(arr2)]

  return res.length
}

console.log(ab([[1, 1, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 0, 1,]]));